// Ofri Keidar 302933981 group 05

package model.apartment;

import java.util.Map;

/**
 * The class represents a garden apartment, inherits from class Apartment
 */
public class GardenApartment extends Apartment {
	
	/**
	 * Checks that the parameters for a new garden apartment are of correct format 
	 */
	public static class GardenApartmentParameterChecker extends ParameterChecker {
		
		/**
		 * Parses common apartment's parameters using base class's checker 
		 * and checks the rest of the parameters 
		 */
		public boolean parseParameters(Map<String, String> parameters) {
			
			// check if value given for each garden apartment parameter
			if (parameters.get(GARDEN_AREA) == null || parameters.get(SEPARATE_ENTRANCE) == null) {
				
				return false;
			}
			
			// check basic parameters correctness
			if (!super.parseParameters(parameters)) {
				
				// basic extraction went wrong, stop parsing
				return false;
			}
			
			try {
			
				// check if value given for garden area is a non-negative numeric value
				double checkNumericValue = Double.parseDouble(parameters.get(GARDEN_AREA));
				if (checkNumericValue < 0) {
					// extra unit size cannot be negative
					return false;
				}
							
				// check if boolean value for separate entrance is given
				if (!"true".equals(parameters.get(SEPARATE_ENTRANCE)) && !"false".equals(parameters.get(SEPARATE_ENTRANCE))) {				
					// no separate entrance indicator
					return false;
				}
	
				// parameters are of correct format
				return true;
				
			} catch (NumberFormatException e) {
				// at least one of numeric parameters was not inserted correctly
				return false;
			}
		} 
	}

	private static final short FLOOR = 1; // a garden apartment is always on the first floor
	private boolean hasSepEntrance; // TRUE if the apartment has a separated entrance, FALSE if not

	/**
	 * Constructor- initializes a new garden apartment according to given values
	 * @param roomNum- garden apartment's room number
	 * @param size- garden apartment's size
	 * @param gardenSize- garden apartment's garden size
	 * @param resName- garden apartment's resident name
	 * @param sepEntrance- indicator for separate entrance
	 * @param apartmentNum- apartment's number in building
	 * @param bldID- apartment's building ID
	 * @param address- apartment's building address
	 */
	public GardenApartment(double roomNum, double size, double gardenSize,
			String resName, boolean sepEntrance, int apartmentNum, int bldID,
			String address) {
		// initialize attributes according to given values
		super(FLOOR, roomNum, size, gardenSize, resName, apartmentNum, bldID, address); // use base class's constructor
		this.hasSepEntrance = sepEntrance;
	}
		
	/**
	 * Constructor- initializes a new garden apartment according to given values. 
	 * Used when no resident name is given
	 * @param roomNum- garden apartment's room number
	 * @param size- garden apartment's size
	 * @param gardenSize- garden apartment's garden size
	 * @param sepEntrance- indicator for separate entrance
	 */
	public GardenApartment(double roomNum, double size, double gardenSize, boolean sepEntrance) {
		// initialize attributes according to given values
		super(FLOOR, roomNum, size, gardenSize); // use base class's constructor
		this.hasSepEntrance = sepEntrance;
	}
	
	/**
	 * Returns indicator for separate entrance
	 * @return True if there is a separate entrance, False if not
	 */
	public boolean hasSepEntrance() { return hasSepEntrance; }

	/**
	 * Returns garden area, using base class's extra unit getter 
	 * @return- garden's area attribute
	 */
	public double getGardenArea() { return getExtraUnit(); }
	
	/**
	 * Sets garden area as given value
	 * @param newArea- new area for garden
	 */
	public void setGardenArea(double newArea) { setExtraUnit(newArea); }
	
	/**
	 * Calculates garden apartment's total area as sum of garden apartment's size and garden 
	 */
	public double totalArea() {
		// sum size and garden area and return result
		return getSize() + getExtraUnit(); 
	}
	
	/**
	 * Returns garden apartment's printing format (as a String object) using base class's toString()
	 */
	public String toString() {		
		// use Apartment class's string format to create output
		return "Type: garden apartment, " + super.toString();		
	}
}